﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication6
{
    public partial class _Default : Page
    {
        private static DataTable gvDataTable = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            //页面第一次加载的时候，填充数据
            if (!IsPostBack)
            {
                FillData();
            }
        }
        //为GridView填充数据
        private void FillData()
        {
            string sql = "select * from Hobbies";
            var dt = DbHelper.DataTable(sql);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        //编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();
        }
        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var Id = GetValues(e.RowIndex, 0);
            var HobName = GetValues(e.RowIndex, 1);
            var HobSex = GetValues(e.RowIndex, 2);
            var HobAge = GetValues(e.RowIndex, 3);
            var Hobby = GetValues(e.RowIndex, 4);
            var Remarks = GetValues(e.RowIndex, 5);

            var isOk = int.TryParse(Id, out int id);

            if (isOk)
            {
                //更新
                var sql = string.Format(" update Hobbies set HobName = '{0}',HobSex='{1}',HobAge ='{2}',Hobby='{3}',Remarks='{4}' where Id = '{5}'", HobName, HobSex, HobAge, Hobby, Remarks, Id);
                DbHelper.AddOrUpadateOrData(sql);
            }
            else
            {
                //添加
                var sql = string.Format(" insert into Hobbies(HobName ,HobSex ,HobAge,Hobby,Remarks)values('{0}','{1}','{2}','{3}','{4}')", HobName, HobSex, HobAge, Hobby, Remarks);
                DbHelper.AddOrUpadateOrData(sql);
            }
            GridView1.EditIndex = -1;
            FillData();
        }
        //取值
        private string GetValues(int RowIndex, int ColIndex)
        {
            var cell = GridView1.Rows[RowIndex].Cells[ColIndex];

            string value = string.Empty;
            if (cell.Controls.Count > 0)
            {
                var t = cell.Controls[0];
                var txtBox = (TextBox)t;
                value = txtBox.Text;
            }
            else
            {
                value = cell.Text;
            }
            return value;
        }

        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var Id = GetValues(e.RowIndex, 0);

            var sql = string.Format(" delete from Hobbies where Id = '{0}'", Id);
            DbHelper.AddOrUpadateOrData(sql);

            GridView1.EditIndex = -1;
            FillData();
        }
        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillData();
        }
        //添加
        protected void 你是憨货_Click(object sender, EventArgs e)
        {
            //DataTable data = new DataTable();
            //data.Columns.Add(new DataColumn("Id"));
            //data.Columns.Add(new DataColumn("HobName"));
            //data.Columns.Add(new DataColumn("HobSex"));
            //data.Columns.Add(new DataColumn("HobAge"));
            //data.Columns.Add(new DataColumn("Hobby"));
            //data.Columns.Add(new DataColumn("Remarks"));

            //foreach (GridViewRow row in GridView1.Rows)
            //{
            //    var newRow = data.NewRow();
            //    newRow["Id"] = GetValues(row.RowIndex, 0).Replace("&nbsp;", "");
            //    newRow["HobName"] = GetValues(row.RowIndex, 1).Replace("&nbsp;", "");
            //    newRow["HobSex"] = GetValues(row.RowIndex, 2).Replace("&nbsp;", "");
            //    newRow["HobAge"] = GetValues(row.RowIndex, 3).Replace("&nbsp;", "");
            //    newRow["Hobby"] = GetValues(row.RowIndex, 4).Replace("&nbsp;", "");
            //    newRow["Remarks"] = GetValues(row.RowIndex, 5).Replace("&nbsp;", "");

            //    data.Rows.Add(newRow);
            //}
            //data.Rows.Add(data.NewRow());

            //GridView1.EditIndex = data.Rows.Count - 1;

            //GridView1.DataSource = data;
            //GridView1.DataBind();

            var sql = "select * from Hobbies";
            gvDataTable = DbHelper.DataTable(sql);

            var dt = gvDataTable.Copy();


            gvDataTable = dt;

            dt.Rows.Add(dt.NewRow());

            GridView1.DataSource = gvDataTable;

            GridView1.EditIndex = dt.Rows.Count - 1;

            GridView1.DataSource = gvDataTable;
            GridView1.DataBind();

        }
    }
}